In [12]:
import random
toss = random.random() # returns a random value between 0 and 1
if toss > 0.5:
print 'I won'
In [11]:
toss = random.random()
if toss > 0.5:
print 'I won'
else:
print 'You won'
In [19]:
fruits = ['apple', 'orange', 'banana', 'water melon']
fruit_index = random.randint(0, 3) # Get a random number between 0 and length of the fruit list
fruit = fruits[fruit_index] # use fruit_index as an index to randomly select a fruit
if fruit == 'apple':
print 'red'
elif fruit == 'orange':
print 'orange'
elif fruit == 'banana':
print 'yellow'
else:
print 'green'
In [ ]: